package ims.servlet;

import ims.beans.CustomerOrderSummary;
import ims.db.search.custOrder.SearchCustOrderDAO;
import ims.db.search.custOrder.SearchCustOrderDbRequest;
import ims.db.search.custOrder.SearchCustOrderDbResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SearchCustOrderServlet
 */
@WebServlet("/SearchCustOrderServlet")
public class SearchCustOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchCustOrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			response.setContentType("text/html;charset=UTF-8");
	        PrintWriter out = response.getWriter();
	        
	        //generating output of page
			out.println("<html>");
            out.println("<head>");
            out.println("<title>Customer Order Search</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<a href='/IMS/prodType'>Product Type</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/product'>Product</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/account.jsp'>Update Accounts</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/reports.jsp'>Reports</a></br></br>");
            out.println("<h1>Customer Order Search </h1>");
            
            out.println("<form name='viewCustOrderSummaryForm' action='/IMS/searchCustOrder' method='post'>" );
            out.println("Order Number: <input type='text' name='orderNum' size='20' maxlength='20' /></br>");
            out.println("Customer first name: <input type='text' name='custFName' size='20' maxlength='40' />Customer last name: <input type='text' name='custLName' size='20' maxlength='40' /></br>");
            out.println("Order Date (MM-DD-YYYY): <input type='text' name='orderDt' size='10' maxlength='10' /></br>");
			out.println("Order Type: <select name='orderType'>");
			out.println("<option value='All' >All</option>" );
            out.println("<option value='Purchase'>Purchase</option>" );
            out.println("<option value='Return'>Return</option>" );
            out.println("<option value='Exchange'>Exchange</option>" );
            out.println("<option value='Cancelled'>Cancelled</option>" );
            out.println("</select> </br>");
            out.println("<input type='submit' value='Search' />");
            out.println(" </form> " );
            out.println(" </body> " );
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			response.setContentType("text/html;charset=UTF-8");
	        PrintWriter out = response.getWriter();
	        
	        //generating output of page
			out.println("<html>");
            out.println("<head>");
            out.println("<title>Customer Order Search</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<a href='/IMS/prodType'>Product Type</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/product'>Product</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/account.jsp'>Update Accounts</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/reports.jsp'>Reports</a></br></br>");
            out.println("<h1>Customer Order Search </h1>");
            
            out.println("<form name='viewCustOrderSummaryForm' action='/IMS/searchCustOrder' method='post'>" );
            out.println("Order Number: <input type='text' name='orderNum' size='20' maxlength='20' /></br>");
            out.println("Customer first name: <input type='text' name='custFName' size='20' maxlength='40' />Customer last name: <input type='text' name='custLName' size='20' maxlength='40' /></br>");
            out.println("Order Date (MM-DD-YYYY): <input type='text' name='orderDt' size='10' maxlength='10' /></br>");
			out.println("Order Type: <select name='orderType'>");
			out.println("<option value='All' >All</option>" );
            out.println("<option value='Purchase'>Purchase</option>" );
            out.println("<option value='Return'>Return</option>" );
            out.println("<option value='Exchange'>Exchange</option>" );
            out.println("<option value='Cancelled'>Cancelled</option>" );
            out.println("</select> </br>");
            out.println("<input type='submit' value='Search' />");
            out.println(" </form></br></br>" );
            
            out.println("<table border='1'>");
            out.println("<tr><th>Order Number</th><th>Order Date</th><th>Customer Name</th><th>Order Type</th></tr>");
            
            //Getting order summaries
            SearchCustOrderDbRequest dbRequest = new SearchCustOrderDbRequest();
            dbRequest.setCustFname(request.getParameter("custFName"));
            dbRequest.setCustLname(request.getParameter("custLName"));
            dbRequest.setOrderDt(request.getParameter("orderDt"));
            dbRequest.setOrderNum(request.getParameter("orderNum"));
            dbRequest.setOrderType(request.getParameter("orderType"));
            
            SearchCustOrderDAO db = new SearchCustOrderDAO();
            SearchCustOrderDbResponse dbResponse = (SearchCustOrderDbResponse)db.execute(dbRequest);
            
            ArrayList<CustomerOrderSummary> orders = dbResponse.getOrders();
            for(CustomerOrderSummary order: orders)
            {
            	out.println("<tr>");
            	out.println("<td><a href='/IMS/viewOrder?orderNum="+order.getOrderNum()+"'>"+order.getOrderNum()+"</a></td>");
            	out.println("<td>"+order.getOrderDt()+"</td>");
            	out.println("<td>"+order.getCustName()+"</td>");
            	out.println("<td>"+order.getOrderType()+"</td>");
            	out.println("</tr>");
            }
            
            
            out.println("</table>");
            out.println(" </body> " );
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
